package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.ILinkmanRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Linkman;
import cn.tedu.antconlonysever.contract.pojo.param.LinkmanAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.LinkmanUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.LinkmanListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.LinkmanStandardVO;
import cn.tedu.antconlonysever.contract.service.ILinkmanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class LinkmanServiceImpl implements ILinkmanService {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ILinkmanRepository linkmanRepository;

    @Override
    public void addNew(LinkmanAddNewParam linkmanAddNewParam) {
        Long cusId = linkmanAddNewParam.getCusId();
        int countCusId = linkmanRepository.countCusId(cusId);
        if(countCusId>0){
            String message = "添加联系人失败，联系人客户id已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Linkman linkman=new Linkman();
        BeanUtils.copyProperties(linkmanAddNewParam,linkman);
        linkman.setGmtCreate(LocalDateTime.now());
        linkman.setGmtModified(LocalDateTime.now());
        int rows = linkmanRepository.insert(linkman);
        if(rows!=1){
            String message = "添加联系人失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void delete(Long id) {
        LinkmanStandardVO standardById = linkmanRepository.getStandardById(id);
        if (standardById==null) {
            String message = "删除联系人失败，联系人ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = linkmanRepository.deleteById(id);
        if(rows!=1){
            String message = "删除联系人失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, LinkmanUpdateParam linkmanUpdateParam) {
        LinkmanStandardVO standardById = linkmanRepository.getStandardById(id);
        if (standardById==null) {
            String message = "修改联系人失败，联系人ID不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int cusIdNotId = linkmanRepository.contactCusIdNotId(id, linkmanUpdateParam.getCusId());
        if(cusIdNotId>0){
            String message = "修改联系人失败，尝试修改联系人已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Linkman linkman =new Linkman();
        BeanUtils.copyProperties(linkmanUpdateParam,linkman);
        linkman.setId(id);
        int rows = linkmanRepository.update(linkman);
        if(rows!=1){
            String message = "修改联系人失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);

        }

    }

    @Override
    public LinkmanStandardVO getStandardById(Long id) {
        LinkmanStandardVO queryResult = linkmanRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询联系人详情失败，联系人详情数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);

        }
        return queryResult;
    }

    @Override
    public PageData<LinkmanListVO> list(Integer pageNum) {
        PageData<LinkmanListVO> pageData = linkmanRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<LinkmanListVO> list(Integer pageNum, Integer pageSize) {
        PageData<LinkmanListVO> pageData = linkmanRepository.list(pageNum, pageSize);
        return pageData;
    }
}
